package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.SpuDao;
import com.cxyd.modules.wms.dto.SpuDTO;
import com.cxyd.modules.wms.entity.SpuEntity;
import com.cxyd.modules.wms.service.SpuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

/**
 * 商品信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@Service
public class SpuServiceImpl extends CrudServiceImpl<SpuDao, SpuEntity, SpuDTO> implements SpuService {
    private final SysUserService userService;

    public SpuServiceImpl(SysUserService userService) {
        this.userService = userService;
    }

    @Override
    public PageData<SpuDTO> page(Map<String, Object> params) {
        //1.查询物料信息
        final IPage<SpuEntity> page = baseDao.selectPage(
                getPage(params, "spu_id", true),
                getWrapper(params)
        );
        //2.获取用户信息
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.getRecords().forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });

        return getPageData(page, SpuDTO.class);
    }

    @Override
    public QueryWrapper<SpuEntity> getWrapper(Map<String, Object> params) {
        //查询条件
        String spuNo = (String) params.get("spuNo");
        String spuName = (String) params.get("spuName");
        String contType = (String) params.get("contType");
        String spuSpec = (String) params.get("spuSpec");

        QueryWrapper<SpuEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        wrapper.eq(StringUtils.isNotBlank(contType), "cont_type", contType);
        wrapper.like(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.like(StringUtils.isNotBlank(spuName), "spu_name", spuName);
        wrapper.like(StringUtils.isNotBlank(spuSpec), "spu_spec", spuSpec);

        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(@RequestBody Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            deleteByLocateId(ids[i]);
        }
    }

    @Override
    public SpuEntity findBySpuNo(String spuNo) {
        QueryWrapper<SpuEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(SpuEntity::getSpuNo,spuNo);
        return baseDao.selectOne(qw);
    }

    @Override
    public SpuEntity getSpuBySpuNo(String spuNo) {
        QueryWrapper<SpuEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(SpuEntity::getSpuNo,spuNo);
        return baseDao.selectOne(qw);
    }

    public void deleteByLocateId(Long id) {
        baseDao.deleteById(id);
    }
}